package com.hongxun.design.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.jungong.model.MaterialDetail;
import com.hongxun.design.jungong.model.MatreialDetailSys;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.lidroid.xutils.http.RequestParams;
import com.pop.android.common.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class AddNewMaterial extends BaseActivity {


    private List<MaterialDetail> detailList = new ArrayList<>();
    private List<MaterialDetail> allList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_material);
        findViewById(R.id.imageButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getListAll();
    }

    public void add(View view) {
        String bm = ((EditText) findViewById(R.id.bm)).getText().toString().trim();
        String lb = ((EditText) findViewById(R.id.lb)).getText().toString().trim();
        String mc = ((EditText) findViewById(R.id.mc)).getText().toString().trim();
        String xh = ((EditText) findViewById(R.id.xh)).getText().toString().trim();
        String sl = ((EditText) findViewById(R.id.sl)).getText().toString().trim();
        String dw = ((EditText) findViewById(R.id.dw)).getText().toString().trim();
        if (dw.isEmpty()) {
            ToastUtils.showToast(this, "单位不能为空");
            return;
        } else if (dw.length() > 32) {
            ToastUtils.showToast(this, "单位长度大于32个字");
            return;
        }
        if (lb.isEmpty()) {
            ToastUtils.showToast(this, "物料分类不能为空");
            return;
        }
        if (bm.isEmpty()) {
            ToastUtils.showToast(this, "物料编码不能为空");
            return;
        } else if (bm.length() > 64) {
            ToastUtils.showToast(this, "物料编码大于64个字");
            return;
        }
        if (mc.isEmpty()) {
            ToastUtils.showToast(this, "名称不能为空");
            return;
        } else if (mc.length() > 64) {
            ToastUtils.showToast(this, "名称大于64个字");
            return;
        }
        if (sl.isEmpty()) {
            ToastUtils.showToast(this, "未录入数量");
            return;
        }
        if (xh.length() > 100) {
            ToastUtils.showToast(this, "规格型号大于100个字");
            return;
        }
        int sort = 0;
        for (MaterialDetail m : allList) {
            if (m.getMaterial_number().equals(bm)) {
                ToastUtils.showToast(this, "物料编码不能重复");
                return;
            }
            if (m.getType().equals(lb)) {
                sort = m.getSort();
            }
        }
        MaterialDetail detail = new MaterialDetail();
        detail.setType(lb);
        detail.setSort(sort);//新添加的默认为0
        detail.setMaterial_number(bm);
        detail.setName(mc);
        detail.setUnit(dw);
        detail.setNum(sl);
        detail.setSpec(xh);
        detailList.clear();
        detailList.add(detail);
        upData();
    }

    void getListAll() {
        String name = MyApp.currProject.getProject_name().trim();
        if (name.isEmpty()) return;
        String versionId = MyApp.currProject.getCodeset_version_id().trim();
        if (versionId.isEmpty()) {
            ToastUtils.showToast(this, "请先手动上传项目材料");
            return;
        }
        MyApp.HTTP.getMaterialListAll(handler, 2, "", "", versionId);
    }

    private void upData() {
        showWaitDialog(-1, "处理中...");
        RequestParams params = new RequestParams();
        params.addBodyParameter("pro_id", MyApp.currProject.getStage_id());
        params.addBodyParameter("addNew", "add");
        params.addBodyParameter("pro_name", MyApp.currProject.getProject_name());
        String jsonString = JSON.toJSONString(detailList);
        params.addBodyParameter("material_all", jsonString);
        MyApp.HTTP.uploadProjectMateial(handler, params);
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case HttpStringUtil.SUCCESSED:
                    hideWaitIngDialog();
                    String valueOf = String.valueOf(msg.obj);
                    //正确的时候(不含code，msg)
                    JSONObject object = JSONObject.parseObject(valueOf);
                    if (FastJsonUtil.JsonToCheck(valueOf)) {
                        if (object.containsKey("codesetVersionid")) {
                            String ver_ID = object.getString("codesetVersionid");
                            MyApp.currProject.setCodeset_version_id(ver_ID);
                            ToastUtils.showToast(getApplicationContext(), "材料添加成功，可继续添加或返回上级菜单");
                            MyApp.HTTP.getMaterialList1(handler, 3, "", "", ver_ID, 0, 999);
                        } else {
                            String msg0 = "未获取到codesetVersionid";
                            if (object.containsKey("msg")) {
                                msg0 = object.getString("msg");
                            }
                            ToastUtils.showToast(getApplicationContext(), msg0);
                        }
                    } else {
                        String msg0 = "材料添加失败";
                        if (object.containsKey("msg")) {
                            msg0 = object.getString("msg");
                        }
                        ToastUtils.showToast(getApplicationContext(), msg0);
                    }
                    break;
                case 2:
                    if (null != msg.obj) jsonToData(String.valueOf(msg.obj));
                    break;
                case 3://nx单独新增材料后获取项目全部材料缓存到xml
                    List<MatreialDetailSys> toGetT = FastJsonUtil.getMaterialList(String.valueOf(msg.obj));
                    ShardPreferencesTool.save(getApplicationContext(), MString.nxMaterialImp, FastJsonUtil.beanToJson(toGetT), 3);
                    break;
            }
            return false;
        }
    });


    private void jsonToData(String json) {
        List<MatreialDetailSys> toGetT = FastJsonUtil.getMaterialList(json);
        for (MatreialDetailSys detailSys : toGetT) {
            MaterialDetail detail = new MaterialDetail();
            detail.setNum(String.valueOf(detailSys.getNum()));
            detail.setUnit(String.valueOf(detailSys.getUnit()));
            detail.setSpec(String.valueOf(detailSys.getSpec()));
            detail.setMaterial_number(String.valueOf(detailSys.getErp_code()));
            detail.setName(String.valueOf(detailSys.getName()));
            detail.setName(String.valueOf(detailSys.getName()));
            detail.setType(String.valueOf(detailSys.getType()));
            detail.setSort(detailSys.getSort());
            allList.add(detail);
        }
    }

}
